const Koa = require('koa')
const app = new Koa()
const cors = require('koa2-cors')
const views = require('koa-views')
//const json = require('koa-json')
const onerror = require('koa-onerror')
const body = require('koa-body')
const logger = require('koa-logger')
//引入路由文件
const admin = require('./routes/titlePage/admin')
const free = require('./routes/runer/free')
const adminLogin = require('./routes/behind/adminLogin')
const insertContent = require('./routes/behind/insertContent')
const insertgoods = require('./routes/behind/insertgoods')
const content = require('./routes/homePage/content')


const funzf = require('./routes/runer/fun/fun05')
const funo = require('./routes/runer/fun/fun1')
const funof = require('./routes/runer/fun/fun15')
const funt = require('./routes/runer/fun/fun2')
const funtf = require('./routes/runer/fun/fun25')
const remind= require('./routes/titlePage/remind')
const login = require('./routes/titlePage/login')
const together = require('./routes/runer/together')

const goods = require('./routes/mall/goods')
const user = require('./routes/titlePage/user')
const address = require('./routes/mine/address')
const collectArticles = require('./routes/homePage/collectArticles')
const deletegoods = require('./routes/behind/deletegoods')
const updategoods = require('./routes/behind/updategoods')

const selectlogin = require('./routes/behind/selectlogin')
const fuzzyquery = require('./routes/homePage/fuzzyquery')
const adviseupdateObj = require('./routes/behind/updateadvice')
const delectadvice = require('./routes/behind/delectadvice')
const deletecontent = require('./routes/behind/deletecontent')
const fuzzyquerycontent = require('./routes/behind/fuzzyquerycontent')

const updatepassword = require('./routes/behind/updatepassword')
const deleteaddress = require('./routes/mine/deleteaddress')
const advise = require('./routes/runer/advise') 
const advice = require('./routes/behind/advice')
const collected = require('./routes/homePage/collected')
const get_together = require('./routes/runer/get_together')
const image = require('./routes/behind/image')
const fuzzyquerygoods = require('./routes/behind/fuzzyquerygoods')
// error handler
onerror(app)

// middlewares，添加中间件的过程
app.use(body({
  enableTypes:['json', 'form', 'text']
}))
//app.use(json())
// app.use(logger())
//静态资源解析
//app.use(require('koa-static')(__dirname + '/public'))

// app.use(views(__dirname + '/views', {
//   extension: 'ejs'
// }))

// logger
app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  //ctx.status = 200;//强行改动
  console.log('status', ctx.status)
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

app.use(cors({
  origin: function (ctx) {
      return '*'
  }
}))


// routes
app.use(admin.routes(), admin.allowedMethods())
app.use(adminLogin.routes(), adminLogin.allowedMethods())
app.use(insertContent.routes(), insertContent.allowedMethods())
app.use(insertgoods.routes(),insertgoods.allowedMethods())
app.use(free.routes(), free.allowedMethods())

app.use(funzf.routes(), funzf.allowedMethods())
app.use(funo.routes(), funo.allowedMethods())
app.use(funof.routes(), funof.allowedMethods())
app.use(funt.routes(), funt.allowedMethods())
app.use(funtf.routes(), funtf.allowedMethods())

app.use(remind.routes(), remind.allowedMethods())
app.use(login.routes(),remind.allowedMethods())
app.use(content.routes(),content.allowedMethods())
app.use(together.routes(),together.allowedMethods())

app.use(goods.routes(),goods.allowedMethods())
app.use(user.routes(),user.allowedMethods())
app.use(address.routes(),address.allowedMethods())
app.use(collectArticles.routes(),collectArticles.allowedMethods())
app.use(deletegoods.routes(),deletegoods.allowedMethods())
app.use(updategoods.routes(),updategoods.allowedMethods())
app.use(delectadvice.routes(),delectadvice.allowedMethods())
app.use(deletecontent.routes(),deletecontent.allowedMethods())

app.use(selectlogin.routes(),selectlogin.allowedMethods())
app.use(fuzzyquerycontent.routes(),fuzzyquerycontent.allowedMethods())
app.use(fuzzyquery.routes(),fuzzyquery.allowedMethods())
app.use(adviseupdateObj.routes(),adviseupdateObj.allowedMethods())
app.use(advise.routes(),advise.allowedMethods())
app.use(advice.routes(),advice.allowedMethods())
app.use(collected.routes(),collected.allowedMethods())
app.use(get_together.routes(),get_together.allowedMethods())
app.use(image.routes(),image.allowedMethods())

app.use(updatepassword.routes(),updatepassword.allowedMethods())
app.use(deleteaddress.routes(),deleteaddress.allowedMethods())
app.use(fuzzyquerygoods.routes(),fuzzyquerygoods.allowedMethods())
// error-handling
app.on('error', (err, ctx) => {
  console.error('server error', err, ctx)
});

process.env.PORT = 2005

module.exports = app
